package com.sendtion.xrichtextdemo.db;

import com.sendtion.xrichtextdemo.bean.Group;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.app.Context;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.resultset.ResultSet;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

/**
 * GroupDao
 *
 * @since 2021-06-22
 */
public class GroupDao {
    private static final String BASE_URI = "dataability://com.sendtion.xrichtextdemo.MyOpenHelper1";
    private static final String DATA_PATH = "/person";
    private static final String STRING = "db_group";
    private DataAbilityHelper dataAbilityHelper;

    /**
     * GroupDao
     *
     * @param context
     */
    public GroupDao(Context context) {
        dataAbilityHelper = DataAbilityHelper.creator(context);
    }

    /**
     * 查询所有分类列表
     *
     * @param context
     * @return List
     */
    public List<Group> queryGroupAll(Context context) {
        DataAbilityPredicates dataAbilityPredicates = new DataAbilityPredicates();
        List<Group> groupList = new ArrayList<Group>();
        Group group = null;
        ResultSet cursor = null;
        try {
            cursor = dataAbilityHelper.query(Uri.parse(BASE_URI + DATA_PATH), new String[]{}, dataAbilityPredicates);
            while (cursor.goToNextRow()) {
                int groupId = cursor.getInt(cursor.getColumnIndexForName("g_id"));
                String groupName = cursor.getString(cursor.getColumnIndexForName("g_name"));
                int order = cursor.getInt(cursor.getColumnIndexForName("g_order"));
                String color = cursor.getString(cursor.getColumnIndexForName("g_color"));
                int encrypt = cursor.getInt(cursor.getColumnIndexForName("g_encrypt"));
                String createTime = cursor.getString(cursor.getColumnIndexForName("g_create_time"));
                String updateTime = cursor.getString(cursor.getColumnIndexForName("g_update_time"));
                group = new Group();
                group.setId(groupId);
                group.setName(groupName);
                group.setOrder(order);
                group.setColor(color);
                group.setIsEncrypt(encrypt);
                group.setCreateTime(createTime);
                group.setUpdateTime(updateTime);
                groupList.add(group);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return groupList;
    }

    /**
     * 根据分类ID查询分类
     *
     * @param groupId
     * @return Group
     */
    public Group queryGroupById(int groupId) {
        DataAbilityPredicates dataAbilityPredicates = new DataAbilityPredicates();

        Group group = null;
        ResultSet cursor = null;
        try {
            cursor = dataAbilityHelper.query(Uri.parse(BASE_URI + DATA_PATH), new String[]{groupId + ""},
                    dataAbilityPredicates);
            while (cursor.goToNextRow()) {
                int order = cursor.getInt(cursor.getColumnIndexForName("g_order"));
                String color = cursor.getString(cursor.getColumnIndexForName("g_color"));
                int encrypt = cursor.getInt(cursor.getColumnIndexForName("g_encrypt"));
                String groupName = cursor.getString(cursor.getColumnIndexForName("g_name"));
                String createTime = cursor.getString(cursor.getColumnIndexForName("g_create_time"));
                String updateTime = cursor.getString(cursor.getColumnIndexForName("g_update_time"));
                group = new Group();
                group.setId(groupId);
                group.setName(groupName);
                group.setOrder(order);
                group.setColor(color);
                group.setIsEncrypt(encrypt);
                group.setCreateTime(createTime);
                group.setUpdateTime(updateTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
            if (cursor != null) {
                cursor.close();
            }
        }
        return group;
    }
}
